﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mal.Web.Security.DataModel;
using System.Collections.Specialized;
using System;

namespace Mal.Web.Security.Provider
{
    public class SqlPermissionProvider : PermissionProvider
    {
        private WebSecurityDataContext dataContext;
        ///<summary>get/set  </summary>
        public string ConnectionString { get; private set; }

        //public SqlPermissionProvider()
        //{
        //    Initialize(string.Empty, string.Empty);
        //}

        public SqlPermissionProvider(string connectionString)
        {
            Initialize(string.Empty, connectionString);
        }

        public SqlPermissionProvider(string applicationName, string connectionString)
        {
            Initialize(applicationName, connectionString);
        }

        public void Initialize(string applicationName, string connectionString)
        {
            //this.Name = "SqlPermissionProvider";
            //this.Description = "Sql Permission Provider";
            this.ConnectionString = connectionString;
            this.ApplicationName = applicationName;
        }        

        public override bool UserHasPermission(string userName, string permissionName)
        {
            if (string.IsNullOrEmpty(userName))
            {
                ArgumentNullException exp = new ArgumentNullException("userName");
                throw exp;
            }

            if (string.IsNullOrEmpty(permissionName))
            {
                ArgumentNullException exp = new ArgumentNullException("permissionName");
                throw exp;
            }

            using (WebSecurityDataContext dataContext = new WebSecurityDataContext(this.ConnectionString))
            {
                int result = dataContext.mal_UserHasPermission(ApplicationName, userName, permissionName);

                if (result == 1)
                    return true;
                else
                    return false;
            }
        }

        public override bool Authenticate(string userName, string password)
        {
            if (string.IsNullOrEmpty(userName))
            {
                ArgumentNullException exp = new ArgumentNullException("userName");
                throw exp;
            }

            if (string.IsNullOrEmpty(password))
            {
                ArgumentNullException exp = new ArgumentNullException("password");
                throw exp;
            }

            using (WebSecurityDataContext dataContext = new WebSecurityDataContext(this.ConnectionString))
            {
                int result = dataContext.mal_UserAuthenticate(this.ApplicationName, userName, password);

                if (result == 1)
                    return true;
                else
                    return false;
            }
        }

        public override Guid? GetUserId(string userName)
        {
            if (string.IsNullOrEmpty(userName))
            {
                ArgumentNullException exp = new ArgumentNullException("userName");
                throw exp;
            }           

            using (WebSecurityDataContext dataContext = new WebSecurityDataContext(this.ConnectionString))
            {
                List<mal_GetUserIdResult> results = dataContext.mal_GetUserId(this.ApplicationName, userName).ToList();

                if (results.Count>0)
                    return results[0].UserId;
                else
                    return null;
            }
        }
    }
}
